package org.akomantoso.core.data;

import java.io.IOException;

import org.akomantoso.core.data.attribute.Source;
import org.akomantoso.core.io.Externalizable;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;

/**
 * <pre>
 * <xsd:element name="identification">
 * 	 <xsd:complexType>
 * 		<xsd:sequence>
 * 			<xsd:element ref="FRBRWork"/>
 * 			<xsd:element ref="FRBRExpression"/>
 * 			<xsd:element ref="FRBRManifestation"/>
 * 			<xsd:element ref="FRBRItem" minOccurs="0" maxOccurs="1"/>
 * 		</xsd:sequence>
 * 	  <xsd:attributeGroup ref="source"/>
 *   </xsd:complexType>
 * </xsd:element>
 * </pre>
 * 
 * <p>
 * The attribute source links to the agent (person, organization) providing the specific annotation or markup
 * </p>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 */
public final class Identification implements Source , Externalizable{

	public static final String ELEMENT = "identification".intern();
	
	// Mandatory
	private final FRBRWork _FRBRWork = new FRBRWork();

	// Mandatory
	private final FRBRExpression _FRBRExpression = new FRBRExpression();
	
	// Mandatory
	private final FRBRManifestation _FRBRManifestation = new FRBRManifestation();
	
	// Optional
	private FRBRItem _FRBRItem;

	// Mandatory
	private String _source;

	public FRBRWork getFRBRWork() {
		return _FRBRWork;
	}

	public FRBRExpression getFRBRExpression() {
		return _FRBRExpression;
	}

	public FRBRManifestation getFRBRManifestation() {
		return _FRBRManifestation;
	}

	public FRBRItem getFRBRItem() {
		return _FRBRItem;
	}

	public void setFRBRItem(FRBRItem fRBRItem) {
		_FRBRItem = fRBRItem;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Source#getSource()
	 */
	public String getSource() {
		return _source;
	}

	/**
	 * @see org.akomantoso.core.data.attribute.Source#setSource(java.lang.String)
	 */
	public void setSource(String source) {
		_source = source;
	}

	
	/**
	 * @see org.akomantoso.core.io.Externalizable#writeExternal(org.akomantoso.core.io.Output)
	 */
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeSource(out, this);
		_FRBRWork.writeExternal(out);
		_FRBRExpression.writeExternal(out);
		_FRBRManifestation.writeExternal(out);
		if (_FRBRItem != null) {
			_FRBRItem.writeExternal(out);
		}
		out.writeEnd();
	}
	

	/**
	 * @see org.akomantoso.core.io.Externalizable#readExternal(org.akomantoso.core.io.Input)
	 */
	public <T extends Input> void readExternal(T in) throws IOException {
		_source = in.readAttribute(Source.ATTRIBUTE);
		
		in.readNextStartElement();
		
		if (FRBRWork.ELEMENT == in.getLocalName()) {
			_FRBRWork.readExternal(in);
		}
		
		if (FRBRExpression.ELEMENT == in.getLocalName()) {
			_FRBRExpression.readExternal(in);
		}
		
		if (FRBRManifestation.ELEMENT == in.getLocalName()) {
			_FRBRManifestation.readExternal(in);
		}
		
		if (FRBRItem.ELEMENT == in.getLocalName()) {
			_FRBRItem.readExternal(in);
		}
	}
	
}